Skip to content

Commit

Permalink
Mouse Fix and Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Oct 11, 2023
1 parent f91da2a commit 1a94786
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 64 deletions.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ kernel = obj/kernel/loader.o \
obj/kernel/gui/desktop.o \
obj/kernel/gui/font.o \
obj/kernel/gui/widgets/text.o \
obj/kernel/gui/widgets/button.o \
obj/kernel/common/graphicsContext.o \
obj/kernel/common/colour.o \
obj/kernel/common/inputStream.o \
Expand Down Expand Up @@ -191,7 +192,8 @@ runQ_W: maxOS.iso
debugQ: build
x-terminal-emulator -e make runQ QEMU_EXTRA_PARAMS="-s -S" & gdb -ex 'set remotetimeout 300' -ex 'target remote localhost:1234' -ex 'symbol-file maxOS.sym'


guiDebugQ: build
x-terminal-emulator -e make runQ QEMU_EXTRA_PARAMS="-s -S -curses" & gdb -ex 'set remotetimeout 300' -ex 'target remote localhost:1234' -ex 'symbol-file maxOS.sym' -tui

install_dep:
sudo apt-get update -y
Expand Down
Binary file added docs/Screenshots/WTF/Button Text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions docs/commit.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# OOP Events - 3/10/23
- changed events to be in a more OOP style
- for now old functions are kept for backwards compatibility but may be removed in the future
# Buttons - 3/10/23
- Fixed the error with the mouse being weird and not sending click events properly, Window was passing X,Y to the mouse instead of mouseX, mouseY. (X,Y are keycodes that were included with the keyboard driver)
- Fixed button releasing error
2 changes: 1 addition & 1 deletion kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ set(
${fs_h}/fat32.h ${fs_c}/fat32.cpp
${fs_h}/filesystem.h ${fs_c}/filesystem.cpp

include/common/rectangle.h include/common/coordinates.h include/common/pair.h include/common/constants.h include/common/time.h include/drivers/clock/clock.h src/drivers/clock/clock.cpp src/common/inputStream.cpp include/common/inputStream.h src/common/outputStream.cpp include/common/outputStream.h include/gui/font.h src/gui/font.cpp include/system/multiboot.h include/drivers/video/vesa.h src/drivers/video/vesa.cpp include/drivers/console/console.h src/drivers/console/console.cpp include/drivers/console/textmode.h src/drivers/console/textmode.cpp include/drivers/console/vesaboot.h src/drivers/console/vesaboot.cpp include/common/logo.h include/common/eventHandler.h)
include/common/rectangle.h include/common/coordinates.h include/common/pair.h include/common/constants.h include/common/time.h include/drivers/clock/clock.h src/drivers/clock/clock.cpp src/common/inputStream.cpp include/common/inputStream.h src/common/outputStream.cpp include/common/outputStream.h include/gui/font.h src/gui/font.cpp include/system/multiboot.h include/drivers/video/vesa.h src/drivers/video/vesa.cpp include/drivers/console/console.h src/drivers/console/console.cpp include/drivers/console/textmode.h src/drivers/console/textmode.cpp include/drivers/console/vesaboot.h src/drivers/console/vesaboot.cpp include/common/logo.h include/common/eventHandler.h include/gui/widgets/button.h src/gui/widgets/button.cpp)
#Ignore standard library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -Wl,nostdlib -Wl,nodefaultlibs")

Expand Down
78 changes: 78 additions & 0 deletions kernel/include/gui/widgets/button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// Created by 98max on 10/10/2023.
//

#ifndef MAXOS_GUI_WIDGETS_BUTTON_H
#define MAXOS_GUI_WIDGETS_BUTTON_H

#include <common/types.h>
#include <common/eventHandler.h>
#include <gui/widget.h>
#include <gui/font.h>


namespace maxOS {

namespace gui {

namespace widgets {

//forward declaration
class Button;

enum ButtonEvents{
BUTTON_PRESSED,
BUTTON_RELEASED
};

class ButtonPressedEvent : public common::Event<ButtonEvents>{
public:
ButtonPressedEvent(Button* source);
~ButtonPressedEvent();

Button* source;
};

class ButtonReleasedEvent : public common::Event<ButtonEvents>{
public:
ButtonReleasedEvent(Button* source);
~ButtonReleasedEvent();

Button* source;
};

class ButtonEventHandler : public common::EventHandler<ButtonEvents>{
public:
ButtonEventHandler();
~ButtonEventHandler();

virtual void onEvent(common::Event<ButtonEvents>* event);

virtual void onButtonPressed(Button* source);
virtual void onButtonReleased(Button* source);
};

class Button : public Widget, public common::EventManager<ButtonEvents> {

public:
Button(common::int32_t left, common::int32_t top, common::uint32_t width, common::uint32_t height, common::string text);
~Button();

// Widget Stuff
void draw(common::GraphicsContext* gc, common::Rectangle<int>& area);
drivers::peripherals::MouseEventHandler* onMouseButtonPressed(common::uint32_t x, common::uint32_t y, common::uint8_t button);
void onMouseButtonReleased(common::uint32_t x, common::uint32_t y, common::uint8_t button);

// Button Stuff
common::Colour backgroundColour;
common::Colour borderColour;
gui::AmigaFont font;
common::string text;

};
}
}

}

#endif //MAXOS_GUI_WIDGETS_BUTTON_H
6 changes: 3 additions & 3 deletions kernel/include/gui/widgets/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by 98max on 10/16/2022.
//

#ifndef MAXOS_GUI_TEXT_H
#define MAXOS_GUI_TEXT_H
#ifndef MAXOS_GUI_WDIGETS_TEXT_H
#define MAXOS_GUI_WDIGETS_TEXT_H

#include <common/types.h>
#include <common/graphicsContext.h>
Expand Down Expand Up @@ -40,4 +40,4 @@ namespace maxOS {
}
}

#endif //MAXOS_GUI_TEXT_H
#endif //MAXOS_GUI_WDIGETS_TEXT_H
42 changes: 21 additions & 21 deletions kernel/src/drivers/peripherals/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ MouseDriver::MouseDriver(InterruptManager* manager)
dataPort(0x60),
commandPort(0x64)
{
offest = 2; //The mouse is weird and won't write to exactly 0 sometimes, so there has to be different offsets for different os-es
offest = 2;
buttons = 0;
}
MouseDriver::~MouseDriver(){
Expand Down Expand Up @@ -118,32 +118,32 @@ uint32_t MouseDriver::HandleInterrupt(uint32_t esp){
buffer[offest] = dataPort.Read(); //Read mouse info into buffer
offest = (offest + 1) % 3; //Move through the offset

if(offest == 0)//If the mouse data transmission is complete (3rd piece of data is through)
{
//If the mouse data transmission is incomplete (3rd piece of data isn't through)
if(offest != 0)
return esp;

// If the mouse is moved (buffer 1 and 2 store x and y)
if(buffer[1] != 0 || buffer[2] != 0)
raiseEvent(new MouseMoveEvent(buffer[1], -buffer[2])); // Flip the y axis
// If the mouse is moved (buffer 1 and 2 store x and y)
if(buffer[1] != 0 || buffer[2] != 0)
raiseEvent(new MouseMoveEvent(buffer[1], -buffer[2])); // Flip the y axis

//Detect button press
for (int i = 0; i < 3; ++i) {
//Detect button press
for (int i = 0; i < 3; ++i) {

//Check if it's the same as the previous becuase if the current state of the buttons is not equal to the previous state of the buttons , then the button must have been pressed or released
if((buffer[0] & (0x1<<i)) != (buttons & (0x1<<i)))
{
//This if condition is true if the previous state of the button was set to 1 (it was pressed) , so now it must be released as the button state has changed
if(buttons & (0x1<<i))
raiseEvent(new MouseUpEvent(i + 1));
else
raiseEvent(new MouseDownEvent(i + 1));

//Check if it's the same as the previous becuase if the current state of the buttons is not equal to the previous state of the buttons , then the button must have been pressed or released
if((buffer[0] & (0x01 << i)) != (buttons & (0x01<<1)))
{
//This if condition is true if the previous state of the button was set to 1 (it was pressed) , so now it must be released as the button state has changed
if(buttons & (0x1<<i))
raiseEvent(new MouseUpEvent(i + 1));
else
raiseEvent(new MouseDownEvent(i + 1));

}
}

// Update the buttons
buttons = buffer[0];
}

// Update the buttons
buttons = buffer[0];

return esp;
}

Expand Down
39 changes: 11 additions & 28 deletions kernel/src/gui/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,27 +540,21 @@ void CompositeWidget::onMouseMoveWidget(common::uint32_t fromX, common::uint32_t
*/
peripherals::MouseEventHandler *CompositeWidget::onMouseButtonPressed(uint32_t x, uint32_t y, uint8_t button) {

MouseEventHandler* mouseEventHandler = nullptr;
MouseEventHandler* mouseEventHandler = 0;

// Loop through the children
for(Vector<Widget*>::iterator childWidget = children.begin(); childWidget != children.end(); childWidget++){

// Get the position of the child
Rectangle<int> childArea = (*childWidget)->getPosition();

// Check if the mouse is in the child
if(childArea.contains(x, y)){

// Get the position of the mouse relative to the child
uint32_t childX = x - childArea.left;
uint32_t childY = y - childArea.top;
// If the mouse was clicked inside the child
if((*childWidget)->containsCoordinate(x, y)){

// Call the child's onMouseButtonPressed function
mouseEventHandler = (*childWidget)->onMouseButtonPressed(childX, childY, button);
// Pass the event to the child
mouseEventHandler = (*childWidget)->onMouseButtonPressed(x - (*childWidget)->position.left, y - (*childWidget)->position.top, button);

// Break as the event has been handled
break;
}

}

// Return the mouseEventHandler
Expand All @@ -580,26 +574,15 @@ void CompositeWidget::onMouseButtonReleased(uint32_t x, uint32_t y, uint8_t butt
// Loop through the children
for(Vector<Widget*>::iterator childWidget = children.begin(); childWidget != children.end(); childWidget++){

// Get the position of the child
Rectangle<int> childArea = (*childWidget)->getPosition();
// If the mouse was clicked inside the child
if((*childWidget)->containsCoordinate(x, y)){

// Check if the mouse is in the child
if(childArea.contains(x, y)){

// Get the position of the mouse relative to the child
uint32_t childX = x - childArea.left;
uint32_t childY = y - childArea.top;

// Call the child's onMouseButtonReleased function
(*childWidget)->onMouseButtonReleased(childX, childY, button);
// Pass the event to the child
(*childWidget)->onMouseButtonReleased(x - (*childWidget)->position.left, y - (*childWidget)->position.top, button);

// Break as the event has been handled
break;
}

}
}





Loading

0 comments on commit 1a94786

Please sign in to comment.