Dear Cerberus X User!
As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.
Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.
Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!
Accepting ensures:
- Continued access to your account with a short break for the actual transfer.
- Retention of your data under the same terms.
Without consent:
- You don't have further access to your forum user account.
- Your account and personal data will be deleted after April 5, 2024.
- Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.
We hope to keep you in our community and see you on the forum soon!
All the best
Your Cerberus X Team
Import mojo
Function Main();New MyApp;end
Class MyApp Extends App
Field but:Image
Field but_state:Int=0
Method OnCreate()
but=LoadImage("image.png")
End
Method OnUpdate()
If MouseX<but.Width() And MouseY<but.Height()
but_state=1
If MouseHit() Then but_state=2;Print "Do Something!"
Else
but_state=0
Endif
End
Method OnRender()
Cls()
Select but_state
Case 0 ; SetAlpha(0.8)
Case 1 ; SetAlpha(1)
Case 2 ; SetColor(255,0,0);DrawRect(0,0,1000,1000)
End
DrawImage(but,0,0)
End
End
Hey, I’m fairly new to cx and coding in general and I was wondering if you can make a png image as a button so when it is clicked with the mouse it changes the game state
Hey, I’m fairly new to cx and coding in general and I was wondering if you can make a png image as a button so when it is clicked with the mouse it changes the game state
Strict
Import mojo
Import common
Import input
Import button
Class CGame Extends App
Method OnCreate:Int()
SetUpdateRate(60)
CreateButton(GUI_BUTTON_HELLO, 100, 200, 100, 25, " HELLO ")
CreateButton(GUI_BUTTON_GOODBYE, 205, 200, 100, 25, " GOODBYE ")
Return 0
End Method
Method OnUpdate:Int()
Local buttonID:Int
PollInput()
buttonID = PollButtons()
Select buttonID
Case GUI_BUTTON_HELLO
Print "HELLO"
Case GUI_BUTTON_GOODBYE
Print "GOOD BYE"
End Select
Return 0
End Method
Method OnRender:Int()
Cls
RenderButtons()
DrawText("X:"+DevicePointerX()+", Y:"+DevicePointerY(), DevicePointerX()+10, DevicePointerY() )
Return 0
End Method
End Class
Function Main:Int()
New CGame()
Return 0
End Function
Strict
Import mojo
Import common
' Read the device controls and set the device button status flag
Function PollInput:Void()
CInputDevice.device.status = 0
CInputDevice.device.pointer.XPos = MouseX()
CInputDevice.device.pointer.YPos = MouseY()
' Map a mouse button to a bit in the button status flag. Using bitwise OR(|) sets a bit in the button status flag.
If MouseDown(MOUSE_LEFT) CInputDevice.device.status |= CTL_BUTTON_ONE
If MouseDown(MOUSE_RIGHT) CInputDevice.device.status |= CTL_BUTTON_TWO
If MouseDown(MOUSE_MIDDLE) CInputDevice.device.status |= CTL_BUTTON_THREE
End Function
Function CheckInput:Bool(buttons:Int=CTL_BUTTON_NONE)
' Using the button mask passed bitwise AND(&) it to the device button status flag
' If the corresponding bit in the button mask is turned on in the device button status flag, then return a boolean value of true
If CInputDevice.device.status & buttons Return True
Return False
End Function
Function DevicePointer:CPoint()
Return CInputDevice.device.pointer
End Function
Function DevicePointerX:Float()
Return CInputDevice.device.PointerX()
End Function
Function DevicePointerY:Float()
Return CInputDevice.device.PointerY()
End Function
' Simple wrapper class to encapsulate a control device status.
Class CInputDevice
Method Clear:Void()
status = 0
End Method
Method PointerX:Float()
Return pointer.XPos()
End Method
Method PointerY:Float()
Return pointer.YPos()
End Method
Private
Global device:= New CInputDevice()
Field status:Int
Field pointer:= New CPoint(DeviceWidth()/2, DeviceHeight()/2)
End Class
Strict
Import mojo
Import common
Import input
' Create a button and add it to the collection container so that it can be drawn or polled with the next two function below.
Function CreateButton:CButton(id:Int, x:Float, y:Float, w:Int=100, h:Int=25, text:String="Button")
Local b:= New CButton(id, x, y, w, h, text)
CButton.buttons.Add(id, b)
Return b
End Function
Function RenderButtons:Void()
For Local i:=Eachin CButton.buttons
' We have to get the value of an item in the container first before you can call the items Draw method.
i.Value().Draw()
Next
End Function
Function PollButtons:Int()
For Local i:=Eachin CButton.buttons
If i.Value().ButtonCheck() Return i.Value().ButtonID()
Next
Return GUI_BUTTON_NONE
End Function
Class CButton
Method New(id:Int, x:Float=0, y:Float=0, w:Int=1, h:Int=1, text:String="Button")
Self.pos.XPos = x
Self.pos.YPos = y
Self.w = w
Self.h = h
Self.state = BTNSTAT_NORMAL
Self.id = id
Self.text = text
End Method
Method ButtonCheck:Bool()
state=BTNSTAT_NORMAL
' Note to test for multiple button inputs you can chain the constants together using the bitwise OR(|) operator.
' CTL_BUTTON_ONE|CTL_BUTTON_TWO
Local input:= CheckInput(CTL_BUTTON_ONE)
If Point2RectBounds(DevicePointerX(), DevicePointerY(), pos.XPos, pos.YPos, pos.XPos+w, pos.YPos+h)
' Any non zero value retrieved will equate to a boolean true value
If input
' Set the GUI buttons state to pressed and save this state
state = BTNSTAT_DOWN
lastState = state
Else
' If the last state was the GUI button pressed and there was no input, then set the button to it's non pressed state and
' trigger that the button was pressed by returning a boolean true value.
If lastState = BTNSTAT_DOWN And input = 0
lastState = BTNSTAT_NORMAL
state = lastState
Return True
End
' If there in no input state and the device cursor is over a button, the highlight the button.
state = BTNSTAT_HOVER
lastState = state
Endif
Else
state = BTNSTAT_NONE
lastState = state
Endif
Return False
End Method
Method Draw:Void()
Select state
Case BTNSTAT_DOWN
SetColor(100, 100, 100)
Case BTNSTAT_HOVER
SetColor(255, 255, 255)
Default
SetColor(175, 175, 175)
End Select
DrawRect(pos.XPos, pos.YPos, w, h)
DrawText(text, pos.XPos+(w/2), pos.YPos+(h/2), 0.5, 0.5)
End Method
' Returns the button id that was set when creating the button.
Method ButtonID:Int()
Return id
End Method
Private
Const BTNSTAT_NONE:=-1
Const BTNSTAT_NORMAL:=0
Const BTNSTAT_DOWN:=1
Const BTNSTAT_HOVER:=2
Global buttons:= New IntMap<CButton>()
Field pos:=New CPoint()
Field w:Int
Field h:Int
Field state:Int
Field lastState:Int
Field id:Int
Field text:String
End Class
Strict
' Button masks to trap a bit in the device button status flag.
Const CTL_BUTTON_NONE:=0
Const CTL_BUTTON_ONE:=$01
Const CTL_BUTTON_TWO:=$02
Const CTL_BUTTON_THREE:=$04
' Consts for the button id's
Const GUI_BUTTON_NONE:=0
Const GUI_BUTTON_HELLO:=1
Const GUI_BUTTON_GOODBYE:=2
' This is a custom data type the represents s point in 2D (screen) space.
Class CPoint
Method New(x:Float, y:Float)
Self.x = x
Self.y = y
End Method
Method XPos:Void(x:Float) Property
Self.x = x
End Method
Method XPos:Float() Property
Return x
End Method
Method YPos:Void(y:Float) Property
Self.y = y
End Method
Method YPos:Float() Property
Return y
End Method
Method GetPoint:CPont()
Return Self
End Method
Private
Field x:Float
Field y:Float
End Class
#Rem This is a general purpose function to check if a point is within a rectangle.
x and y is the position of the point in 2D space
x1 and y1 is the top left corner of the rectangle.
#End x2 and y2 is the bottom right of the rectangle and would be the rectangles x positon + width and the rectangles y position + height
Function Point2RectBounds:Bool(x:Float, y:Float, x1:Float, y1:Float, x2:Float, y2:Float)
If x > x1 And x < x2 And y > y1 And y < y2 Then Return True
Return False
End Function