1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
unit imguimain;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, Controls, Graphics, ExtCtrls, OpenGLContext, LazLogger,
GL, GLExt, LCLType, fpImage, SysUtils;
type
{ TGLColor }
TGLColor = record
alpha: GLushort;
blue: GLushort;
green: GLushort;
red: GLushort;
end;
{ TForm1 }
TForm1 = class(TForm)
OpenGLControl1: TOpenGLControl;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
procedure FormKeyPress(Sender: TObject; var Key: char);
procedure OpenGLControl1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure OpenGLControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
procedure OpenGLControl1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
procedure OpenGLControl1Paint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ private declarations }
first: Boolean;
public
{ public declarations }
end;
{ UIStateType }
UIStateType = object
activeitem: integer;
hotitem: integer;
kbditem: integer;
keychar: integer;
keyentered: integer;
keymod: TShiftState;
lastwidget: integer;
mousedown: integer;
mousex: integer;
mousey: integer;
procedure Init;
procedure SetMousePos(X, Y: integer);
end;
var
Form1: TForm1;
uistate: UIStateType;
implementation
uses
uglyfont;
var
bgcolor: integer;
sometext: string;
genid: integer;
{ UIStateType }
procedure UIStateType.Init;
begin
mousex := 0;
mousey := 0;
mousedown := 0;
hotitem := 0;
activeitem := 0;
kbditem := 0;
keyentered := 0;
keymod := [];
lastwidget := 0;
end;
procedure UIStateType.SetMousePos(X, Y: integer);
begin
mousex := X;
mousey := Y;
end;
{$R *.lfm}
function GLColor(color: GLuint): TGLColor;
begin
Result.red := (color shr 16) and $0000ff;
Result.green := (color shr 8) and $0000ff;
Result.blue := (color shr 0) and $0000ff;
//Result.alpha := (color shr 0) and $000000ff;
Result.alpha := $ff;
end;
//Draw the string. Characters are fixed width, so this is also
//deadly simple.
procedure drawstring(str: string; x, y: double);
begin
glTextOut(x, y + 14, 0, 14, 14, 1, 0, str);
end;
//Simplified interface to OpenGL's fillrect call
procedure drawrect(x, y, w, h: integer; AColor: TGLColor);
begin
glColor3ub(AColor.red, AColor.green, AColor.blue);
glRectf(x, y, x + w, y + h);
end;
//Check whether current mouse position is within a rectangle
function regionhit(x, y, w, h: integer): integer;
begin
if (uistate.mousex < x) or
(uistate.mousey < y) or
(uistate.mousex >= x + w) or
(uistate.mousey >= y + h) then
Result := 0
else
Result := 1;
end;
//Simple button IMGUI widget
function button(id: integer; x: integer; y: integer): integer;
begin
//Check whether the button should be hot
if regionhit(x, y, 64, 48) = 1 then
begin
uistate.hotitem := id;
if (uistate.activeitem = 0) and (uistate.mousedown = 1) then
uistate.activeitem := id;
end;
//If no widget has keyboard focus, take it
if uistate.kbditem = 0 then
uistate.kbditem := id;
//If we have keyboard focus, show it
if uistate.kbditem = id then
drawrect(x - 6, y - 6, 84, 68, GLColor($ff0000));
drawrect(x + 8, y + 8, 64, 48, GLColor($000000));
//Render button
if uistate.hotitem = id then
begin
if uistate.activeitem = id then
//Button is both 'hot' and 'active'
drawrect(x + 2, y + 2, 64, 48, GLColor($ffffff))
else
//Button is merely 'hot'
drawrect(x, y, 64, 48, GLColor($ffffff));
end
else
//button is not hot, but it may be active
drawrect(x, y, 64, 48, GLColor($aaaaaa));
//If we have keyboard focus, we'll need to process the keys
if uistate.kbditem = id then
begin
case uistate.keyentered of
VK_TAB:
begin
//If tab is pressed, lose keyboard focus.
//Next widget will grab the focus.
uistate.kbditem := 0;
//If shift was also pressed, we want to move focus
//to the previous widget instead.
if ssShift in uistate.keymod then
uistate.kbditem := uistate.lastwidget;
//Also clear the key so that next widget
//won't process it
uistate.keyentered := 0;
end;
VK_RETURN:
begin
//Had keyboard focus, received return,
//so we'll act as if we were clicked.
Result := 1;
exit;
end;
end;
end;
uistate.lastwidget := id;
//If button is hot and active, but mouse button is not
//down, the user must have clicked the button.
if (uistate.mousedown = 0) and
(uistate.hotitem = id) and
(uistate.activeitem = id) then
Result := 1
else
//Otherwise, no clicky.
Result := 0;
end;
//Simple scroll bar IMGUI widget
function slider(id: integer; x: integer; y: integer; max: integer; var Value: integer): integer;
var
ypos: integer;
mousepos: integer;
v: integer;
begin
//Calculate mouse cursor's relative y offset
ypos := ((256 - 16) * Value) div max;
//Check for hotness
if regionhit(x + 8, y + 8, 16, 255) = 1 then
begin
uistate.hotitem := id;
if (uistate.activeitem = 0) and (uistate.mousedown = 1) then
uistate.activeitem := id;
end;
//If no widget has keyboard focus, take it
if uistate.kbditem = 0 then
uistate.kbditem := id;
//If we have keyboard focus, show it
if uistate.kbditem = id then
drawrect(x - 4, y - 4, 40, 280, GLColor($ff0000));
drawrect(x, y, 32, 256 + 16, GLColor($777777));
//Render the scrollbar
if (uistate.activeitem = id) or (uistate.hotitem = id) then
drawrect(x + 8, y + 8 + ypos, 16, 16, GLColor($ffffff))
else
drawrect(x + 8, y + 8 + ypos, 16, 16, GLColor($aaaaaa));
//If we have keyboard focus, we'll need to process the keys
if uistate.kbditem = id then
begin
case uistate.keyentered of
VK_TAB:
begin
//If tab is pressed, lose keyboard focus.
//Next widget will grab the focus.
uistate.kbditem := 0;
//If shift was also pressed, we want to move focus
//to the previous widget instead.
if ssShift in uistate.keymod then
uistate.kbditem := uistate.lastwidget;
//Also clear the key so that next widget
//won't process it
uistate.keyentered := 0;
end;
VK_UP:
begin
//Slide slider up (if not at zero)
if Value > 0 then
begin
Dec(Value);
Result := 1;
exit;
end;
end;
VK_DOWN:
begin
//Slide slider down (if not at max)
if Value < max then
begin
Inc(Value);
Result := 1;
exit;
end;
end;
end;
end;
uistate.lastwidget := id;
//Update widget value
if uistate.activeitem = id then
begin
mousepos := uistate.mousey - (y + 8);
if mousepos < 0 then
mousepos := 0;
if mousepos > 255 then
mousepos := 255;
v := (mousepos * max) div 255;
if v <> Value then
begin
Value := v;
Result := 1;
exit;
end;
end;
Result := 0;
end;
function GetTickCount: DWord;
begin
Result := DWord(Trunc(Now * 24 * 60 * 60 * 1000));
end;
function textfield(id: integer; x: integer; y: integer; var buffer: string): integer;
var
len: integer;
changed: integer;
begin
len := Length(buffer);
changed := 0;
//Check for hotness
if regionhit(x - 4, y - 4, 30 * 14 + 8, 24 + 8) = 1 then
begin
uistate.hotitem := id;
if (uistate.activeitem = 0) and (uistate.mousedown = 1) then
uistate.activeitem := id;
end;
//If no widget has keyboard focus, take it
if uistate.kbditem = 0 then
uistate.kbditem := id;
//If we have keyboard focus, show it
if uistate.kbditem = id then
drawrect(x - 6, y - 6, 30 * 14 + 12, 24 + 12, GLColor($ff0000));
//Render the text field
if (uistate.activeitem = id) or (uistate.hotitem = id) then
drawrect(x - 4, y - 4, 30 * 14 + 8, 24 + 8, GLColor($aaaaaa))
else
drawrect(x - 4, y - 4, 30 * 14 + 8, 24 + 8, GLColor($777777));
glColor3ub($00, $00, $00);
drawstring(buffer, x, y);
//Render cursor if we have keyboard focus
if (uistate.kbditem = id) and (((GetTickCount shr 8) and 1) = 1) then
drawstring('_', x + len * 14, y);
//If we have keyboard focus, we'll need to process the keys
if uistate.kbditem = id then
begin
case uistate.keyentered of
VK_TAB:
begin
//If tab is pressed, lose keyboard focus.
//Next widget will grab the focus.
uistate.kbditem := 0;
//If shift was also pressed, we want to move focus
//to the previous widget instead.
if ssShift in uistate.keymod then
uistate.kbditem := uistate.lastwidget;
uistate.keyentered := 0;
end;
VK_BACK:
begin
//Also clear the key so that next widget
//won't process it
if len > 0 then
begin
Delete(buffer, len, 1);
Dec(len);
changed := 1;
end;
end;
end;
if (uistate.keychar >= 32) and (uistate.keychar < 127) and (len < 30) then
begin
buffer := buffer + Chr(uistate.keyentered);
Inc(len);
changed := 1;
end;
end;
//If button is hot and active, but mouse button is not
//down, the user must have clicked the widget; give it
//keyboard focus.
if (uistate.mousedown = 0) and (uistate.hotitem = id) and (uistate.activeitem = id) then
uistate.kbditem := id;
uistate.lastwidget := id;
Result := changed;
end;
procedure imgui_prepare;
begin
uistate.hotitem := 0;
genid := 0;
end;
procedure imgui_finish;
begin
if uistate.mousedown = 0 then
uistate.activeitem := 0
else
if uistate.activeitem = 0 then
uistate.activeitem := -1;
//If no widget grabbed tab, clear focus
if uistate.keyentered = VK_TAB then
uistate.kbditem := 0;
//Clear the entered key
uistate.keyentered := 0;
uistate.keychar := 0;
end;
function GEN_ID: integer;
begin
Inc(genid);
Result := genid;
end;
//Rendering function
procedure render;
var
slidervalue: integer;
begin
imgui_prepare;
button(GEN_ID, 50, 50);
button(GEN_ID, 150, 50);
if button(GEN_ID, 50, 150) = 1 then
bgcolor := Round(Random * $ffffff);
if button(GEN_ID, 150, 150) = 1 then
halt(0);
textfield(GEN_ID, 50, 250, sometext);
slidervalue := bgcolor and $ff;
if slider(GEN_ID, 500, 40, 255, slidervalue) = 1 then
bgcolor := (bgcolor and $ffff00) or slidervalue;
slidervalue := ((bgcolor shr 10) and $3f);
if slider(GEN_ID, 550, 40, 63, slidervalue) = 1 then
bgcolor := (bgcolor and $ff00ff) or (slidervalue shl 10);
slidervalue := ((bgcolor shr 20) and $f);
if slider(GEN_ID, 600, 40, 15, slidervalue) = 1 then
bgcolor := (bgcolor and $00ffff) or (slidervalue shl 20);
imgui_finish;
end;
{ TForm1 }
procedure TForm1.OpenGLControl1Paint(Sender: TObject);
var
samples, samplebuffers: integer;
begin
if first then begin
glGetIntegerv(GL_SAMPLE_BUFFERS,@samplebuffers);
glGetIntegerv(GL_SAMPLES,@samples);
DebugLn(['SampleBuffers: ',samplebuffers]);
Debugln(['Samples: ',samples]);
first:=false;
end;
//setup 2D projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glOrtho(0, OpenGLControl1.Width, OpenGLControl1.Height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
//clear screen
drawrect(0, 0, 640, 480, GLColor(bgcolor));
render;
glColor3ub($ff, $00, $00);
glTextOut(10, 20, 0, 10, 10, 1, 0, Format('%f FPS', [1000 / OpenGLControl1.FrameDiffTimeInMSecs]));
OpenGLControl1.SwapBuffers;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
OpenGLControl1.Paint;
end;
procedure TForm1.OpenGLControl1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: integer);
begin
uistate.SetMousePos(X, Y);
end;
procedure TForm1.OpenGLControl1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
uistate.mousedown := 1;
end;
procedure TForm1.FormKeyDown(Sender: TObject; var Key: word; Shift: TShiftState);
begin
uistate.keymod := Shift;
uistate.keyentered := Key;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
begin
//if escape is pressed, quit the application
if Ord(Key) = VK_ESCAPE then
halt(0);
uistate.keyentered := Ord(Key);
//if key is ASCII, accept it as character input
if (uistate.keyentered and $FF80) = 0 then
uistate.keychar := uistate.keyentered and $7f;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
bgcolor := $77;
sometext := 'Some text';
first:=true;
end;
procedure TForm1.OpenGLControl1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
uistate.mousedown := 0;
end;
initialization
uistate.Init;
end.
|