You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.2 KiB
117 lines
3.2 KiB
""" |
|
This module defines functions that need to be overwritten |
|
in order for OpenGL to work with the main window. This |
|
module is named the same as the actual widget in order |
|
to make namespacing consistent. |
|
|
|
To be clear, the actual widget is defined in the UI |
|
generated code - `clusterview_ui.py`. The functions |
|
here are imported as overrides to the OpenGL functions of |
|
that widget. |
|
""" |
|
|
|
from enum import Enum |
|
|
|
from OpenGL.GL import (glBegin, glClearColor, glColor4f, glEnable, |
|
glEnd, GL_LIGHT0, GL_LIGHTING, GL_POINTS, |
|
glVertex3f) |
|
|
|
from clusterview.exceptions import InvalidStateError |
|
from clusterview.mode import InvalidMode, Mode |
|
|
|
class Color(Enum): |
|
BLUE = 0 |
|
|
|
# A simple map from Color -> RGBA 4-Tuple |
|
COLOR_TO_RGBA = { |
|
Color.BLUE: (0, 128, 255, 255) |
|
} |
|
|
|
__current_mode = None |
|
__current_event = None |
|
|
|
def set_drawing_mode(mode, event=None): |
|
""" |
|
State management function. It is useful to look at the |
|
different drawing modes as modes in a state machine. |
|
|
|
Calling this function when a mode changes allows the |
|
OpenGL functions to take the correct drawing action |
|
on the OpenGL Widget. |
|
|
|
@param mode The current mode. |
|
@param event The current event (Mostly used for passing coordinates). |
|
""" |
|
if not isinstance(mode, Mode): |
|
raise ValueError("Mode in set_drawing_mode must be of type Mode") |
|
|
|
__current_mode = mode |
|
|
|
if event is not None: |
|
__current_event = event |
|
|
|
|
|
def get_current_mode(): |
|
""" |
|
Returns the current mode according to the OpenGL Widget. |
|
""" |
|
return __current_mode |
|
|
|
|
|
def initialize_gl(): |
|
""" |
|
Initializes the OpenGL context on the Window. |
|
""" |
|
# Since we aren't using shaders lighting needs to be |
|
# enabled. |
|
glEnable(GL_LIGHTING); |
|
glEnable(GL_LIGHT0); |
|
|
|
# Set white background |
|
glClearColor(255, 255, 255, 0) |
|
|
|
|
|
def paint_gl(): |
|
""" |
|
Stock PaintGL function from OpenGL that switches |
|
on the current mode to determine what action to |
|
perform on the current event. |
|
""" |
|
|
|
if (__current_mode in [Mode.ADD, Mode.EDIT, Mode.MOVE, Mode.DELETE] and |
|
__current_event is None): |
|
raise InvalidStateError("Event must exist for ADD, EDIT, MOVE, " + |
|
"and DELETE") |
|
|
|
if __current_mode is Mode.ADD: |
|
raise NotImplementedError("Drawing for ADD not implemented.") |
|
elif __current_mode is Mode.EDIT: |
|
raise NotImplementedError("Drawing for EDIT not implemented.") |
|
elif __current_mode is Mode.MOVE: |
|
raise NotImplementedError("Drawing for MOVE not implemented.") |
|
elif __current_mode is Mode.DELETE: |
|
raise NotImplementedError("Drawing for DELETE not implemented.") |
|
|
|
|
|
def draw_point(x, y, color): |
|
""" |
|
Simple point drawing function. |
|
|
|
Given a coordinate (x, y), and a Color enum this |
|
function will draw the given point with the given |
|
color. |
|
|
|
@param x The x-coordinate. |
|
@param y The y-coordinate. |
|
@param color The Color Enum. |
|
""" |
|
|
|
if not isinstance(color, Color): |
|
raise ValueError("Color must exist in the Color enumeration") |
|
|
|
ct = COLOR_TO_RGBA[color] |
|
|
|
glBegin(GL_POINTS) |
|
glColor4f(ct[0], ct[1], ct[2], ct[3]) |
|
glVertex3f(x, y, 0.0) # Z is currently fixed to 0 |
|
glEnd()
|
|
|