Jump to content
  • entries
    943
  • comments
    5,899
  • views
    924,400

GUI drawn directly on a window


Josh

2,400 views

 Share

This code creates a GUI directly on a window, with no OpenGL rendering context created at all. The same scripts can be used for widgets created on a window, a rendering context, or a texture:

--Initialize Steamworks (optional)
Steamworks:Initialize()

--Set the application title
title="$PROJECT_TITLE"

--Create a window
local windowstyle = window.Titlebar + window.Resizable + window.Hidden
if System:GetProperty("fullscreen")=="1" then windowstyle=windowstyle+window.FullScreen end
window=Window:Create(title,0,0,System:GetProperty("screenwidth","1024"),System:GetProperty("screenheight","768"),windowstyle)

local gui = GUI:Create(window)
gui:GetBase():SetScript("Scripts/GUI/Panel.lua")
local widget = Widget:Create(20,20,75,30,gui:GetBase())
widget:SetScript("Scripts/GUI/Button.lua")

window:Show()

while window:KeyDown(Key.Escape)==false do

--If window has been closed, end the program
if window:Closed() then break end

end

 

You can try the attached executable to see it working. Notice the zip file contains no textures, shaders, etc. because no rendering context is ever initialized.

  • Upvote 6
 Share

8 Comments


Recommended Comments

Definitely do kind of like this way. It's very simple and straight forward, looks organized too. We could just create a listener or something in C++ and attach a Lua script to it to "spawn" the GUI from C++ code right?

Link to comment

It would probably be something like Widget::SetCallback() instead of Widget::SetScript() for C++. The real meat of the GUI is the window management and handling partial window refreshing, widget alignment, and events.

 

Think of it as a game engine for GUI elements.

Link to comment

Have you defined how the user would handle events yet? Say the click event? That button script would be a generic button script that you could reuse with many different buttons right? So the click events inside it wouldn't be what the user uses to do game specific logic in the buttons click event right?

Link to comment

Right, that's what I'm thinking about. Maybe we'll have an event queue system, so that script would call something like this when the button is clicked:

Event::Emit(Event::WidgetAction,self.widget)

 

Main.lua would then add this code in the loop:

while Event::Peek() do
local event = Event::Wait()
if event.id == Event::WidgetAction then
	if event.source == mybutton then
		--do something here
	end
end
end

 

It's kind of weird because there is the internal event handling in the window class, and then we're adding sort of a secondary event system. Not sure I like it but from the user's perspective it makes sense.

 

I really like the abstract design of this. I think the community is going to be very good at churning out a ton of useful widget scripts for various game interfaces I couldn't dream of.

Link to comment

Callbacks, I agree. Cleaner code, easier to separate different functionality and it gets triggered instantly.

Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...