airstruck wrote:Are you suggesting that the stuff that captures user input (from mouse, keyboard, window manager) should be moved into a separate library?
Not necessarily. All I have said is that they should not be on the GUI but
somewhere else. Where this is, exactly, depends on the particular game we're talking about.
What I am saying is that your GUI library should not be concerned about how the mouse, the keyboard, and the pads work, because that is not its
job. It should not be doing that.
Let's talk about what GUI needs, as inputs:
- 4 directional inputs related to the "currently highlighted control" (move the selection up, down, left, right). These are usually the arrow keys in the keyboard and the d-pad in a joystick. But a GUI library should not know that. It just needs someone to tell it "move the highlighted item up". Maybe the programmer wants to simulate that the player is drunk, and changes the inputs on purpose. The GUI can't know that.
- If there is a pointing devise (like a mouse), then the library needs to interact with it. But there might be screen transformations at play (cameras, resolution changes, retina display corrections, etc). So the GUI should not "take over the mouse". It needs someone to tell it "for you, the mouse is here". Maybe in the current game state the GUI does not need to know about the mouse, even if it's there. Maybe the programmer is simulating a software mouse in a tutorial. It is not up to the gui to know that.
- The library needs a way to tell when the currently highlighted control is activated. This could be the "enter key", or the "joystick A button", or the mouse button, or a touch in a touch screen. But again, the library does not need to know this. It just needs someone to tell it "activate the highlighted control now".
- In some particular cases, when dealing with text fields, it also needs to know the keys the user presses. Even in that case, it is not ok that it simply "takes control of the global keyboard". There are scenarios for this: maybe the programmer using the library wants to simulate an NPC using the GUI to perform some action. Maybe there is some sort of "macro" in place by which pressing a button, several controls change, and some input fields are automatically filled up. The point again is that the gui should "present the input for key presses" and allow the programmer to use it, as opposed to "grabbing the global keyboard".
There are other secondary GUI inputs (next/previous highlighted control, go back to previous screen, etc) but I hope you get the idea: A GUI should present inputs, like a gun presents a trigger; It should not "shoot by itself".
Output-wise, there are two main thing a GUI does:
- The first thing is changing game state. This can be done with Lua functions, and I think is not relevant for this particular conversation.
- The second one is drawing itself. But this does not mean that all that love.draw needs to do is drawing the GUI. In games GUIs are quite often drawn along something else. Maybe the gui is a minimap with buttons on the side, like the bottom of a Starcraft game. Maybe the user wants to apply filters to the whole screen (i.e. a screenshake) before drawing the GUI. The GUI must not know about this. It just needs to draw itself when it is told to.
To a GUI lib, the screen, the mouse, the keyboard and the d-pad should be no different than the physics system, the sound system, or the network library - things it should not directly "touch" (unless the player hooks them up some way or the other - via callbacks, an event system provided by another library, or magical rainbows).
This said, it's desirable to include examples about how to hook things up in the docs and demos. You might even provide "default hooking mechanisms for the most obvious options"; but these should be opt-in and easily deactivable.
airstruck wrote:If it were, would that solve the problem with the user having to wire up events or the library having to hook them?
I am not sure I would characterize that as a "problem". To me it sounds like ... programming. It is on the same category as "The user needs to write a for loop every time he wants to iterate over a table". Maybe I am not understanding your concern correctly.