@@ -19,7 +19,7 @@ struct UIState
1919}
2020uistate = {0 , 0 , 0 , 0 , 0 };
2121
22- // Simplified interface to SDL_
22+ // Simplified interface to SDL
2323void drawrect (int x, int y, int w, int h, int color)
2424{
2525 SDL_Rect r;
@@ -30,17 +30,116 @@ void drawrect(int x, int y, int w, int h, int color)
3030 SDL_FillRect (screen, &r, color);
3131}
3232
33+ // Check whether current mouse position is within a rectangle.
34+ int regionhit (int x, int y, int w, int h)
35+ {
36+ if (uistate.mousex < x ||
37+ uistate.mousey < y ||
38+ uistate.mousex > x + w ||
39+ uistate.mousey > y + h)
40+ {
41+ return 0 ;
42+ }
43+ return 1 ;
44+ }
45+
46+ // Simple button IMGUI widget
47+ int button (int id, int x, int y)
48+ {
49+ // Check whether the button should be hot
50+ if (regionhit (x, y, 64 , 48 ))
51+ {
52+ uistate.hotitem = id;
53+ if (uistate.activeitem == 0 && uistate.mousedown )
54+ {
55+ uistate.activeitem = id;
56+ }
57+ }
58+
59+ // Render button -- base button shadow
60+ drawrect (x+8 , y+8 , 64 , 48 , 0 );
61+
62+ // Button is hot.
63+ if (uistate.hotitem == id)
64+ {
65+ if (uistate.activeitem == id)
66+ {
67+ // Button is both 'hot' and 'active' -- change color and position
68+ drawrect (x+2 , y+2 , 64 , 48 , 0xffffff );
69+ } else {
70+ // Button is merely 'hot' -- change color
71+ drawrect (x, y, 64 , 48 , 0xffffff );
72+ }
73+ } else {
74+ // Button is not hot, but it may be active -- normal front color of button
75+ drawrect (x, y, 64 , 48 , 0xaaaaaa );
76+ }
77+
78+ // Check whether user has click the button.
79+
80+ // If button is hot and active, but mouse button is not
81+ // down, the user must have clicked the button.
82+ if (uistate.mousedown == 0 &&
83+ uistate.hotitem == id &&
84+ uistate.activeitem == id)
85+ {
86+ return 1 ;
87+ }
88+
89+ // Otherwise, no clicky.
90+ return 0 ;
91+
92+ }
93+
94+ // Prepare for IMGUI code
95+ void imgui_prepare ()
96+ {
97+ uistate.hotitem = 0 ;
98+ }
99+
100+ // Finish up after IMGUI code
101+ void imgui_finish ()
102+ {
103+ // 如果鼠标左键松开,立刻取消Button的激活状态。
104+ if (uistate.mousedown == 0 )
105+ {
106+ uistate.activeitem = 0 ;
107+ } else {
108+ // if (uistate.activeitem == 0)
109+ // {
110+ // // ???
111+ // uistate.activeitem = -1;
112+ // }
113+ }
114+ }
115+
33116void render ()
34117{
118+ static int bgcolor = 0x77 ;
119+
35120 // Get the window surface
36121 screen = SDL_GetWindowSurface (window);
37122
38123 // Clear the screen.
39- drawrect (0 , 0 , screen_width, screen_height, 0 );
124+ drawrect (0 , 0 , screen_width, screen_height, bgcolor);
125+
126+ imgui_prepare ();
127+
128+ button (2 , 50 , 50 );
129+
130+ button (2 , 150 , 50 );
131+
132+ if (button (3 , 50 , 150 ))
133+ {
134+ bgcolor = (SDL_GetTicks () * 0x0cac01a ) | 0x77 ;
135+ }
136+
137+ if (button (4 , 150 , 150 ))
138+ {
139+ exit (0 );
140+ }
40141
41- // Test the uistate is working.
42- drawrect (uistate.mousex - 32 , uistate.mousey - 24 ,
43- 64 , 48 , 0xff << (uistate.mousedown * 8 ));
142+ imgui_finish ();
44143
45144 // Tell SDL to update the whole screen
46145 SDL_UpdateWindowSurface (window);
0 commit comments