Console-Based Counter with Event-Driven Buttons
This project demonstrates a simple console-based counter application with an event-driven approach to handling button clicks. The main objective is to provide hands-on experience with event listeners in C++ by simulating interactive "Count" and "Reset" buttons within a console window. The project utilizes Windows API for mouse-click detection and centers around three main classes: Counter
, Button
, and Event
.
-
Event Class:
- The
Event
class is designed to manage event listeners, allowing multiple functions to be triggered in response to an event. - With
addListener()
, eachEvent
can store multiple listeners, andtrigger()
activates all listeners, simulating an event-driven model. - This class helps modularize the code, making it easy to add or modify functionality associated with button clicks.
- The
-
Button Class:
- The
Button
class represents a clickable region in the console, specified by its top-left and bottom-right coordinates. - Each button has its own
Event
instance to manage click-related actions through listeners. isInside()
checks if a point (mouse click) falls within the button's area, andOnClick()
activates the button’s associated event listeners, simulating a click action.- Two button instances are created in the main program: one for "Count" and another for "Reset".
- The
-
Counter Class:
- The
Counter
class encapsulates the counter's value and provides methods to increase or reset it. displayDialog()
clears the console and redraws the interface, displaying the current count along with the "Count" and "Reset" button labels.- This class keeps the UI updated with the current counter state after each button click.
- The
-
Counter Interface:
- The program displays the counter value and button labels in a console layout that is redrawn each time a button is clicked.
- The display is formatted using
setw
to align elements, making it visually structured.
-
Event-Driven Button Click Handling:
- In the main loop, the program continuously monitors the mouse position and right-click state.
- When the user right-clicks within the "Count" or "Reset" button area, the appropriate button's event listeners are triggered.
- If "Count" is clicked, the counter increments, and if "Reset" is clicked, the counter resets to zero. Both actions refresh the display.
-
Windows API Integration:
- The program uses
GetCursorPos()
to retrieve the mouse position in screen coordinates andGetAsyncKeyState()
to check for right-clicks. - A debounce mechanism (
Sleep(300)
) prevents repeated triggers from a single click.
- The program uses
- Event-Driven Programming: This project illustrates how to implement an event-driven model in C++ by associating specific actions with button clicks.
- Encapsulation: Separating logic into
Counter
,Button
, andEvent
classes provides a clean, modular structure. - Mouse Interaction in Console Applications: By leveraging Windows API, the project demonstrates how to detect and handle mouse events in a console application.
The interface displays as follows:
+=================================+
| Count = 5 |
+=================================+
+================+================+
| Count | Reset |
+================+================+
This project is ideal for those looking to deepen their understanding of event handling.