Skip to content

Commit

Permalink
Input Box
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Oct 20, 2023
1 parent 1a94786 commit 6d6d1d8
Show file tree
Hide file tree
Showing 13 changed files with 472 additions and 40 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ kernel = obj/kernel/loader.o \
obj/kernel/gui/font.o \
obj/kernel/gui/widgets/text.o \
obj/kernel/gui/widgets/button.o \
obj/kernel/gui/widgets/inputbox.o \
obj/kernel/common/graphicsContext.o \
obj/kernel/common/colour.o \
obj/kernel/common/inputStream.o \
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ Kernel Cleanup
- [x] Console rewrite
- [x] VESA Video Mode
- [x] Kernel Boot Rewrite
- [ ] Rewrite Event Handlers
- [ ] USB
- [ ] Usable Desktop - Windows Resizing is working but mouse is weird
- [x] Rewrite Event Handlers
- [ ] Usable Desktop
- [ ] Better Doxygen Documentation
- [ ] Use better c++ coding conventions
- [ ] Codebase cleanup / rewrite
- [ ] Example Telnet Server (GUI) (EMBEDDED)
- [ ] Fix Filesystem
- [ ] USB
- [ ] HTTP Protocol, DCHP protocol
- [ ] Codebase cleanup / rewrite
- [ ] CMAKE
- [ ] Fix Fat32 Filesystem

Road to Userspace

Expand Down
6 changes: 3 additions & 3 deletions docs/commit.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# 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
# Input Box - 20/10/23
- Added Input Boxes
- Will need to fix focusing and stuff tho
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/gui/widgets/button.h src/gui/widgets/button.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/gui/widgets/button.h src/gui/widgets/button.cpp include/gui/widgets/inputbox.h src/gui/widgets/inputbox.cpp)
#Ignore standard library
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -v -Wl,nostdlib -Wl,nodefaultlibs")

Expand Down
1 change: 1 addition & 0 deletions kernel/include/gui/widgets/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace maxOS {

// Button Stuff
common::Colour backgroundColour;
common::Colour foregroundColour;
common::Colour borderColour;
gui::AmigaFont font;
common::string text;
Expand Down
72 changes: 72 additions & 0 deletions kernel/include/gui/widgets/inputbox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// Created by 98max on 11/10/2023.
//

#ifndef MAXOS_GUI_WIDGETS_INPUTBOX_H
#define MAXOS_GUI_WIDGETS_INPUTBOX_H

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

namespace maxOS{

namespace gui {

namespace widgets {

enum InputBoxEvents{
INPUTBOX_TEXT_CHANGED
};

class InputBoxTextChangedEvent : public common::Event<InputBoxEvents>{
public:
InputBoxTextChangedEvent(common::string newText);
~InputBoxTextChangedEvent();

common::string newText;
};

class InputBoxEventHandler : public common::EventHandler<InputBoxEvents>{
public:
InputBoxEventHandler();
~InputBoxEventHandler();

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

virtual void onInputBoxTextChanged(common::string newText);
};

class InputBox : public Widget, public common::EventManager<InputBoxEvents>{

protected:
char widgetText[256]; // Replace with a buffer in memory later

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

void draw(common::GraphicsContext* gc, common::Rectangle<int>& area);

void onFocus();
void onFocusLost();

void onKeyDown(drivers::peripherals::KeyCode keyDownCode, drivers::peripherals::KeyboardState keyDownState);

void updateText(common::string newText);
common::string getText();

// InputBox Variables
common::Colour backgroundColour;
common::Colour foregroundColour;
common::Colour borderColour;
gui::AmigaFont font;
common::uint32_t cursorPosition;

};
}
}
}

#endif //MAXOS_GUI_WIDGETS_INPUTBOX_H
6 changes: 6 additions & 0 deletions kernel/src/drivers/console/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,12 @@ void ConsoleStream::writeChar(char c) {
case '\0':
break;

// Backspace
case '\b':
// Decrement the x coordinate
cursorX--;
break;

default:
// Put the character on the console
console->putChar(cursorX, cursorY, c);
Expand Down
54 changes: 51 additions & 3 deletions kernel/src/drivers/peripherals/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,62 @@ KeyboardInterpreterEN_US::~KeyboardInterpreterEN_US() {

void KeyboardInterpreterEN_US::onStreamRead(uint8_t scanCode) {

// TODO: Extended ScanCodes E0, E1
int keyType = 0; // Initialize keyType to 0 (0 represents a regular keypress)

// Check if the key is released or pressed (The 8th bit is set to 1 if key is released and cleared to 0 if key is pressed)
bool released = (scanCode & 0x8);
// Check if the key was released (bit 7 set) and certain conditions are met
bool released = (scanCode & 0x80) && (currentExtendedCode1 || (scanCode != 0xe1)) && (nextIsExtendedCode0 || (scanCode != 0xe0));

// If the key is released, clear bit 7 (make it a regular key)
if (released)
scanCode &= ~0x80;

// If the scanCode is 0xe0, it indicates an extended code
if (scanCode == 0xe0)
{
nextIsExtendedCode0 = true; // Set the e0Code flag to true
return;
}

// If e0Code is true, set keyType to 1 and reset e0Code
if (nextIsExtendedCode0)
{
keyType = 1;
nextIsExtendedCode0 = false;

// Check if the scanCode represents a shift key and return (fake shift)
if ((KeyboardInterpreterEN_US::KeyCodeEN_US)scanCode == KeyboardInterpreterEN_US::leftShift || (KeyboardInterpreterEN_US::KeyCodeEN_US)scanCode == KeyboardInterpreterEN_US::rightShift)
return;
}

// If the scanCode is 0xe1, set the e1Code flag to 1 and return
if (scanCode == 0xe1)
{
currentExtendedCode1 = 1;
return;
}

// If e1Code is 1, set e1Code to 2, store the scanCode in e1CodeBuffer, and return
if (currentExtendedCode1 == 1)
{
currentExtendedCode1 = 2;
extendedCode1Buffer = scanCode;
return;
}

// If e1Code is 2, set keyType to 2, reset e1Code, and update e1CodeBuffer
if (currentExtendedCode1 == 2)
{
keyType = 2;
currentExtendedCode1 = 0;
extendedCode1Buffer |= (((uint16_t)scanCode) << 8);
}

bool isShifting = this -> keyBoardState.leftShift || this -> keyBoardState.rightShift;
bool shouldBeUpperCase = isShifting != this -> keyBoardState.capsLock;


// TODO: Probabbly a better way to do this
if(keyType == 0)
switch ((KeyCodeEN_US)scanCode) {

// First row
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/gui/desktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void Desktop::addChild(Widget* childWidget) {
*/
void Desktop::onTime(const Time &time) {

// Check if anything is invaild and needs to be redrawn
// Check if anything is invalid and needs to be redrawn
if(invalidAreas.empty())
return;

Expand Down
4 changes: 4 additions & 0 deletions kernel/src/gui/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ void Widget::onFocusLost() {
*/
void Widget::bringToFront() {

// Bring this widget to the front of the screen
bringToFront(this);

}

/**
Expand Down Expand Up @@ -285,6 +288,7 @@ void Widget::onMouseMoveWidget(common::uint32_t fromX, common::uint32_t fromY, c
* @return nullptr
*/
peripherals::MouseEventHandler* Widget::onMouseButtonPressed(uint32_t x, uint32_t y, uint8_t button) {

// Bring the widget to the front of the screen
bringToFront();

Expand Down
33 changes: 32 additions & 1 deletion kernel/src/gui/widgets/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ ButtonEventHandler::~ButtonEventHandler() {

}

/**
* @details Handles the button events
* @param event The event to handle
*/
void ButtonEventHandler::onEvent(Event<ButtonEvents> *event) {

// Check the event type
Expand All @@ -39,10 +43,18 @@ void ButtonEventHandler::onEvent(Event<ButtonEvents> *event) {

}

/**
* @details Handles the button pressed event
* @param source The source of the event
*/
void ButtonEventHandler::onButtonPressed(Button *source) {

}

/**
* @details Handles the button released event
* @param source The source of the event
*/
void ButtonEventHandler::onButtonReleased(Button *source) {

}
Expand All @@ -58,13 +70,19 @@ Button::Button(int32_t left, int32_t top, uint32_t width, uint32_t height, strin
this -> font = AmigaFont();
this -> text = text;
this -> backgroundColour = Colour(0xFF, 0xFF, 0xFF);
this -> foregroundColour = Colour(0x00, 0x00, 0x00);
this -> borderColour = Colour(0x57, 0x57, 0x57);
}

Button::~Button() {

}

/**
* @details Draws the button
* @param gc The graphics context to draw to
* @param area The area to draw to
*/
void Button::draw(GraphicsContext *gc, Rectangle<int> &area) {

// Default Draw Operation
Expand Down Expand Up @@ -113,10 +131,17 @@ void Button::draw(GraphicsContext *gc, Rectangle<int> &area) {

// Draw the text
common::Rectangle<int> textArea(area.left - 1, area.top - 1, area.width, area.height);
font.drawText(x + 1, y + 1, Colour(0,0,0), backgroundColour, gc, text,textArea);
font.drawText(x + 1, y + 1, foregroundColour, backgroundColour, gc, text,textArea);

}

/**
* @details Handles the mouse button pressed event
* @param x The x position of the mouse
* @param y The y position of the mouse
* @param button The button that was pressed
* @return The mouse event handler
*/
MouseEventHandler* Button::onMouseButtonPressed(uint32_t x, uint32_t y, uint8_t button) {
// Raise the button pressed event
raiseEvent(new ButtonPressedEvent(this));
Expand All @@ -129,6 +154,12 @@ MouseEventHandler* Button::onMouseButtonPressed(uint32_t x, uint32_t y, uint8_t
return Widget::onMouseButtonPressed(x, y, button);
}

/**
* @details Handles the mouse button released event
* @param x The x position of the mouse
* @param y The y position of the mouse
* @param button The button that was released
*/
void Button::onMouseButtonReleased(uint32_t x, uint32_t y, uint8_t button) {

// Raise the button released event
Expand Down
Loading

0 comments on commit 6d6d1d8

Please sign in to comment.