Results 1 to 3 of 3

Thread: Menu example

  1. #1
    Lesser Knight
    Join Date
    Apr 2011
    Location
    Germany
    Posts
    32

    Default Menu example

    A little menu.
    Last edited by Jason Schilling; 07-26-2011 at 06:37 PM.

  2. #2

    Angry

    Nice, but, the sourcecode? no share¿

  3. #3
    Lesser Knight
    Join Date
    Apr 2011
    Location
    Germany
    Posts
    32

    Default

    Here

    Program.cpp
    Code:
    #include "precomp.h"
    
    class Button {
    private:
    
    public:
    	int X;
    	int Y;
    	int Height;
    	int Width;
    	int nWidth;
    	CL_Colorf Color;
    	CL_String Text;
    };
    
    
    class MenuExample {
    private:
    	bool quit;
    	CL_InputDevice mouse,keyboard;
    	CL_GraphicContext gc;
    	std::vector<Button> Buttons;
    	CL_SlotContainer slots;
    
    public:
    	void run(){
    		quit = false;
    
    		CL_DisplayWindow window("MenuExample", 800, 600,false,false);
    
    		slots.connect(window.sig_window_close(),this, &MenuExample::on_window_close);
    		slots.connect(window.get_ic().get_mouse().sig_key_down(),this,&MenuExample::on_mouse_click);
    
    		gc = window.get_gc();
    		keyboard = window.get_ic().get_keyboard();
    		mouse = window.get_ic().get_mouse();
    
    		Button btn1;
    		btn1.X = 0; btn1.Y = 50;
    		btn1.Height = 45; btn1.Width = 200; btn1.nWidth = 200;
    		btn1.Color = CL_Colorf::lightblue;
    		btn1.Text = "This";
    		Buttons.push_back(btn1);
    
    		Button btn2;
    		btn2.X = 0; btn2.Y = 100;
    		btn2.Height = 45; btn2.Width = 200; btn2.nWidth = 200;
    		btn2.Color = CL_Colorf::lightblue;
    		btn2.Text = "Is";
    		Buttons.push_back(btn2);
    
    		Button btn3;
    		btn3.X = 0; btn3.Y = 150;
    		btn3.Height = 45; btn3.Width = 200; btn3.nWidth = 200;
    		btn3.Color = CL_Colorf::lightblue;
    		btn3.Text = "A";
    		Buttons.push_back(btn3);
    
    		Button btn4;												// Init Button
    		btn4.X = 0; btn4.Y = 200;									// Set the Position
    		btn4.Height = 45; btn4.Width = 200; btn4.nWidth = 200;		// Set the Height, Width and the nWidth(normalWidth)
    		btn4.Color = CL_Colorf::lightblue;							// Color from button
    		btn4.Text = "MenuExample";									// Button Text
    		Buttons.push_back(btn4);									// Add Button to vector
    
    		CL_FontDescription font_desc;		
    		font_desc.set_typeface_name("tahoma");
    		font_desc.set_height(11);
    		CL_Font_System font(gc, font_desc);	
    
    		while (!quit){
    			if(keyboard.get_keycode(CL_KEY_ESCAPE) == true){
    				quit = true;
    			}
    			gc.clear(CL_Colorf::black);
    
    			for(std::vector<Button>::iterator iT = Buttons.begin(); iT != Buttons.end(); ++iT){
    				CL_Draw::fill(gc, CL_Rectf((*iT).X, (*iT).Y, (*iT).X+(*iT).Width, (*iT).Y+(*iT).Height), (*iT).Color);			
    				if(MouseIn((*iT).X,(*iT).Y,(*iT).Width,(*iT).Height,mouse.get_x(),mouse.get_y())){
    					(*iT).Width = (*iT).nWidth + 10; // On hovering add 10px on width
    				}else{
    					(*iT).Width = (*iT).nWidth;		 // If the mouse not over the Pos set the nWidth
    													 // You can make a nColor and make the buttons change color on over
    				}
    				font.draw_text(gc, (*iT).X + 10,(*iT).Y+25,(*iT).Text,CL_Colorf::white);
    			}
    
    			window.flip();
    			CL_KeepAlive::process();
    
    			CL_System::sleep(10);
    		}
    	}
    
    	void on_mouse_click(const CL_InputEvent &key, const CL_InputState &state){
    		if(key.id == CL_MOUSE_LEFT){
    			for(std::vector<Button>::iterator iT = Buttons.begin(); iT != Buttons.end(); ++iT){
    				if(MouseIn((*iT).X,(*iT).Y,(*iT).Width,(*iT).Height,key.mouse_pos.x,key.mouse_pos.y)){
    					(*iT).Color = CL_Colorf::blue;
    					if((*iT).Text == "This"){
    						// Do Any on click the "This" button
    					}else if((*iT).Text == "Is"){
    						// Do Any on click the "Is" button
    					}else if((*iT).Text == "A"){
    						// Do Any on click the "A" button
    					}else if((*iT).Text == "MenuExample"){
    						// Do Any on click the "MenuExample" button
    					}
    				}else{
    					(*iT).Color = CL_Colorf::lightblue;
    				}
    			}
    		}
    	}
    
    	bool MouseIn(int X, int Y, int Width, int Height, int mX, int mY){
    		if((mX < X + Width) && (mX > X) && (mY < Y + Height) && (mY > Y)){
    			return true;
    		}else{
    			return false;
    		}
    	}
    
    	void on_window_close(){
    		quit = true;
    	}
    };
    
    class Program
    {
    public:
       static int main(const std::vector<CL_String> &args)
       {
    		CL_SetupCore setup_core;
    		CL_SetupDisplay setup_display;
    		CL_SetupGL setup_gl;
    		CL_SetupGL1 setup_gl1;
    		CL_SetupSWRender setup_swrender;
    
    		try{
               MenuExample Example;
               Example.run();
    		}catch(CL_Exception &exception){
               CL_ConsoleWindow console("Console", 80, 160);
               CL_Console::write_line("Error: " + exception.get_message_and_stack_trace());
               console.display_close_message();
               return -1;
           }
           return 0;
       }
    };
    CL_ClanApplication app(&Program::main);
    Precomp.h
    Code:
    #pragma once
    
    #include <vector>
    #include <ClanLib/core.h>
    #include <ClanLib/display.h>
    #include <ClanLib/gl.h>
    #include <ClanLib/gl1.h>
    #include <ClanLib/swrender.h>
    #include <ClanLib/application.h>
    Precomp.cpp
    Code:
    #include "precomp.h"
    Last edited by Jason Schilling; 07-28-2011 at 11:16 PM.

Similar Threads

  1. Title Screen Menu
    By Kirkkaf in forum Novashell Game Creation System
    Replies: 2
    Last Post: 09-16-2010, 02:15 AM
  2. Menu question?
    By Eng C. Born in forum Official ClanLib SDK Forums
    Replies: 0
    Last Post: 04-04-2009, 07:30 PM
  3. In full screen the menu is off
    By alpharesearch in forum Official ClanLib SDK Forums
    Replies: 5
    Last Post: 01-23-2009, 01:54 PM
  4. Exit to OS rather than worlds menu?
    By Uhfgood in forum Novashell Game Creation System
    Replies: 2
    Last Post: 07-11-2008, 05:33 AM
  5. menu item inconsistencies
    By whisperstorm in forum Novashell Game Creation System
    Replies: 1
    Last Post: 11-24-2006, 07:14 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •