Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

major OVERHAUL #3

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Once you have called this, you can create an **infinite loop** and call two func
```py
while True:
oGUI.startLoop()

...
oGUI.endLoop()
```

*Inbetween* the `start` and `end` loop, you can call the drawing functions.
*Inbetween* the `start` and `end` loop, you should execute the update_gui() *ONCE*.
Then it will render all the gui and handle the callbacks for you

*Here is an example:*
```py
Expand All @@ -29,7 +30,7 @@ checkbox = oGUI.Checkbox(oGUI.gray, oGUI.orange, 125, 150, 20, 20)
while True:
oGUI.startLoop()

checkbox.draw()
oGUI.update_gui()

oGUI.endLoop()
```
Expand All @@ -54,24 +55,73 @@ These are the available colors:
`oGUI.lightgray`
`oGUI.darkgray`

**Functions**
**Creating widgets**
---------------------
Creating *checkboxes*
e.g. Creating *checkboxes*

To create a **checkbox**, we can create a variable and then call the `oGUI.Checkbox()` function. Usage:
```py
checkbox1 = oGUI.Checkbox(outsideColor, insideColor, x position, y position, width, height, enabledByDefault)
checkbox1 = oGUI.Checkbox(outsideColor, insideColor, x position, y position, width, height, enabledByDefault, callback_function)
```
enabledByDefault is optional, and if you leave it blank (dont specify it), it will be false.

We will continue to use *checkbox1* as the *checkbox variable* for the rest of the documentation, and the *rest of these functions* should be called in an **infinite loop.**

To *render* the actual checkbox, we must call its `.draw()` function. Usage:
To *render* the actual checkbox, ~~we must call its `.draw()` function~~
Now, you will only have to run the update_gui() **ONCE** and it will draw *ALL* the widgets you've created(as long as they're not hidden) for you.
So idealy, you won't have to call the draw function manually.
Usage:
```py
checkbox1.draw()
while True:
oGUI.startLoop()

oGUI.update_gui()

oGUI.endLoop()
```
for more widgets creation, goto [example.py](examples/example.py)

**
We need to put this function inbetween of our `startLoop()` and `endLoop()`.

**Callbacks**
---------------------
call back is the core of a gui. This allows a funciton to be called once a widget is interacted in a certain way.
e.g.
```py
import oGUI

oGUI.init()


def button_clicked():
print('I am clicked')

button = oGUI.Button(oGUI.blue, oGUI.white, 400, 300, 100, 30, text='click me', clicked_callback=button_clicked)

while True:
oGUI.startLoop() # Start of Draw Loop

oGUI.update_gui() # handle update and callback

oGUI.endLoop() # End of Draw Loop
```
The function *button_clicked* will be executed everytime the button is clicked.
However, the function will be run in the main thread defaultly,
this means the rest of the program and the mainloop would have to wait for the called function to finish.
So if the function takes up a considerable amount of time, the gui will noticibly stop responding.
To fix this, use multithread libs, so that the callback function and the maintheard can run simultaneously.

Notice that you WON'T want the brackets if you're defining the callback,
Just like in the example, we used button_clicked NOT button_clicked().
The reason is that if you use *funciton*, the function itself will be passed on to the callback
On the contrary, if you use *function()*, the function itself will be executed and its return will be passed on to the callback.
Normally, we don't want this to happen.

More examples of callback can be found in the [example.py](examples/example.py)

**Malipulating widgets**
---------------------
We can also change the *color* of the box if it is hovered over, using the `.is_hovered()` function. Usage:
```py
checkbox1.is_hovered(color)
Expand Down
90 changes: 53 additions & 37 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,67 @@

oGUI.init()

checkbox = oGUI.Checkbox(oGUI.gray, oGUI.orange, 125, 150, 20, 20)
rect = oGUI.Rect(oGUI.darkgray, 100, 100, 300, 500)
box = oGUI.Box(oGUI.lightgray, 100, 100, 300, 500, 5)
button = oGUI.Button(oGUI.gray, oGUI.orange, 120, 200, 30, 30)
button2 = oGUI.Button(oGUI.darkgray, oGUI.lightgray, 368, 103, 30, 35)

myText = oGUI.Text(oGUI.orange, 195, 110, 30, "oGUI Demo")
myText2 = oGUI.Text(oGUI.orange, 155, 152, 25, "Checkbox")
myText3 = oGUI.Text(oGUI.orange, 155, 208, 25, "Button")
myText4 = oGUI.Text(oGUI.black, 375, 105, 30, "X")
def button_clicked():
print('')
print('button is clicked!')
print('this massage would only appear once after the button is clicked')

while True:

oGUI.startLoop() #Start of Draw Loop
def checkbox_status_changed():
print('')
print('checkbox status changed')
print('now, checkbox is:', 'checked' if checkbox.is_enabled() else 'unchecked')
print('this massage would only appear once after the checkbox is toggled')

rect.draw() #Drawing Rectangle, Box, Checkbox, and Button
box.draw()
checkbox.draw()
button.draw()
button2.draw()

myText.draw() #Drawing Text
myText2.draw()
myText3.draw()
myText4.draw()
def exit_button_clicked():
exit(0)

oGUI.endLoop() #End of Draw Loop

checkbox.is_hovered(oGUI.lightgray) #Changes color when checkbox and button(s) is hovered over
button.is_hovered(oGUI.lightgray)
button2.is_hovered(oGUI.gray)
window_x = 100
window_y = 100
window_w = 300
window_h = 500

rect = oGUI.Rect(oGUI.darkgray, window_x, window_y, window_w, window_h)
box = oGUI.Box(oGUI.lightgray, window_x, window_y, window_w, window_h, 5)
checkbox = oGUI.Checkbox(oGUI.gray, oGUI.orange, 125, 150, 20, 20, toggled_callback=checkbox_status_changed,
text='checkbox')
button = oGUI.Button(oGUI.gray, oGUI.orange, 120, 200, text='button', clicked_callback=button_clicked)
quit_button = oGUI.Button(oGUI.darkgray, oGUI.lightgray, 368, 103, 30, 35, clicked_callback=exit_button_clicked,
text='×')

myText = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + 5, 30,
"overlayGUI by ethanedits", textAlign=1)

# DVDCJW

myText.font('Roboto') #Setting Text Object's font
myText2.font('Roboto')
myText3.font('Roboto')
credit_text = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 - 20, 30,
'major overhaul by DVDCJW', textAlign=1, verticalAlign=1)
credit_text2 = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 + 20, 20, 'including:',
textAlign=1, verticalAlign=1)
credit_text3 = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 + 40, 25,
'CALLBACK for button and checkbox', textAlign=1, verticalAlign=1)
credit_text4 = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 + 60, 25, 'widgets upgrade',
textAlign=1, verticalAlign=1)
credit_text5 = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 + 80, 20,
'better checkbox hold logic', textAlign=1, verticalAlign=1)
credit_text6 = oGUI.Text(oGUI.orange, window_x + window_w / 2, window_y + window_h / 2 + 100, 20,
'integrated text for callable widgets', textAlign=1, verticalAlign=1)

myText.dropShadow(oGUI.black, 2) #Setting Text Object's DropShadow
myText2.dropShadow(oGUI.black, 2)
myText3.dropShadow(oGUI.black, 2)

if button.is_enabled(): #Do something if the button is enabled/pressed
print('Button was pressed')
# feel free to delete my credits if you're not comfortable with it.
# But I really made this project way more practical, efficient and maintainable

if button2.is_enabled(): #Exit Button
exit(0)
while True:
oGUI.startLoop() # Start of Draw Loop

oGUI.update_gui() # handle update and callback

# maybe some of your own pygame code if you'd like

if checkbox.is_enabled(): #Do something if the checkbox is enabled
print('Checkbox is Enabled!')
oGUI.endLoop() # End of Draw Loop

checkbox.is_hovered(oGUI.lightgray) # Changes color when checkbox and button(s) is hovered over
button.is_hovered(oGUI.lightgray)
quit_button.is_hovered(oGUI.gray)
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pygame
pypiwin32
Loading