Skip to content

Commit

Permalink
Added text input support and tweaked the new Font system. Welcome to …
Browse files Browse the repository at this point in the history
…version 1.2
  • Loading branch information
Syndelis committed Sep 8, 2020
1 parent 1641eca commit 237299e
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
*.so
eel.c
figure.c
*.txt
shader.c
*.txt
*.png
2 changes: 2 additions & 0 deletions eel.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,6 @@ Functions
"""
cpdef keyPressed(key)
cpdef keyRelease(key)
cdef char _getChar()
cpdef getText()
cpdef mousePressed(int button)
27 changes: 27 additions & 0 deletions eel.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ from glew cimport glewInit, glBindFramebuffer, glFramebufferTexture, glDrawBuffe
from glfw3 cimport *
from gl cimport *
from SOIL cimport *

# Python STDLIB
from ctypes import c_ubyte
# ------------------------------------------------------------------------------
"""
Data Structuresa
Expand All @@ -42,6 +45,8 @@ cdef float ycoord[4]
xcoord[0] = xcoord[1] = ycoord[0] = ycoord[3] = 1
xcoord[2] = xcoord[3] = ycoord[1] = ycoord[2] = 0

# ------------------------------------------------------------------------------
__version__ = 1.2
# ------------------------------------------------------------------------------
"""
Eel's Paintable Superclass
Expand Down Expand Up @@ -297,6 +302,7 @@ cdef class Eel(Paintable):

glfwMakeContextCurrent(self.window)
glfwSetKeyCallback(self.window, keyCallback)
glfwSetCharCallback(self.window, charCallback)
glfwSetMouseButtonCallback(self.window, mouseCallback)
glfwSwapInterval(1)

Expand Down Expand Up @@ -550,6 +556,27 @@ cpdef keyRelease(key):
_keyRelease(key)


cdef char _getChar():
"""
def _getChar()
Returns the last typed character
"""

return _popChar() if _charCount() else 0


cpdef getText():

l = []

cdef unsigned int j = c_ubyte(_getChar()).value
while j:
l.insert(0, chr(j))
j = _getChar()

return "".join(l[::-1])


cpdef mousePressed(int button):
"""
def mousePressed(button)
Expand Down
21 changes: 21 additions & 0 deletions eelCallbacks.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,27 @@ void _keyRelease(int key) {

key_pressed &= (~(1ULL << key));

}
// -----------------------------------------------------------------------------
// TEXT INPUT

void charCallback(GLFWwindow *window, unsigned int codepoint) {

_input[_index] = codepoint;
_index = (_index + 1) % 256;

}

unsigned int _popChar() {

return _index ? _input[--_index] : 0;

}

int _charCount() {

return _index;

}
// -----------------------------------------------------------------------------
// MOUSE INPUT
Expand Down
13 changes: 11 additions & 2 deletions eelCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
// KEYBOARD INPUT
// One for each ASCII-key
static unsigned long long int key_pressed = 0;
static struct _key keybuffer[255];
static char keycount = 0;
// static struct _key keybuffer[255];
// static char keycount = 0;

void keyCallback(GLFWwindow *window, int key, int scan, int action, int mods);
int _keyPressed(int key);
void _keyRelease(int key);
// -----------------------------------------------------------------------------
// TEXT INPUT
static unsigned int _input[256];
static int _index = 0;

void charCallback(GLFWwindow *window, unsigned int codepoint);
unsigned int _popChar();
int _charCount();

// -----------------------------------------------------------------------------
// MOUSE INPUT
static short int mouse_button = 0;
Expand Down
Binary file modified eelCallbacks.o
Binary file not shown.
4 changes: 4 additions & 0 deletions eelCallbacks.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ cdef extern from "eelCallbacks.h":
int _keyPressed(int key)
void _keyRelease(int key)

void charCallback(GLFWwindow *window, unsigned int codepoint)
unsigned int _popChar()
int _charCount()

void mouseCallback(GLFWwindow *window, int button, int action, int mods)
int _mousePressed(int button)
void _mouseRelease(int button)
35 changes: 19 additions & 16 deletions figure.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ Base Text (Cython Implementation)

cdef class _BaseText(_BaseFigure):

cdef char *str
# cdef char *str
cdef int _strlen
cdef Character *font

def __cinit__(self, int x, int y, text, _BaseFont font=None):
Expand All @@ -234,13 +235,15 @@ cdef class _BaseText(_BaseFigure):
self.font = font.font
# self.poly.color = font.color

if type(text) is not bytes: text = bytes(text, "utf-8")
self.str = <char *> malloc(sizeof(char) * strlen(text))
strcpy(self.str, text)
# if type(text) is not bytes: text = bytes(text, "utf-8")
# self.str = <char *> malloc(sizeof(char) * strlen(text))
# strcpy(self.str, text)

self.str = text if type(text) is bytes else bytes(text, "utf-8")
self._strlen = len(self.str)

def __dealloc__(self):
free(self.str)
# def __dealloc__(self):
# free(self.str)


cpdef drawTo(self, Paintable eel):
Expand All @@ -255,9 +258,8 @@ cdef class _BaseText(_BaseFigure):
fx = self.x / width
fy = self.y / height

# for i in range(0, self.chlen):
while (self.str[i]):
ch = self.font + self.str[i]
for i in range(0, self._strlen):
ch = self.font + <char>self.str[i]

xpos = fx + ch.bear.x / width
ypos = fy + (ch.size.y - ch.bear.y) / height
Expand All @@ -283,11 +285,12 @@ cdef class _BaseText(_BaseFigure):


cpdef setText(self, text):
# self.str = text
if type(text) is not bytes: text = bytes(text, "utf-8")
free(self.str)
self.str = <char *> malloc(sizeof(char) * strlen(text))
strcpy(self.str, text)
self.str = text
self._strlen = len(text)
# if type(text) is not bytes: text = bytes(text, "utf-8")
# free(self.str)
# self.str = <char *> malloc(sizeof(char) * strlen(text))
# strcpy(self.str, text)


cpdef getWidth(self):
Expand All @@ -300,7 +303,7 @@ cdef class _BaseText(_BaseFigure):

# for i in range(0, ln):
while (self.str[i]):
ch = self.font + self.str[i]
ch = self.font + <char>self.str[i]
x += (ch.advance >> 6)

i += 1
Expand All @@ -318,7 +321,7 @@ cdef class _BaseText(_BaseFigure):

# for i in range(0, ln):
while (self.str[i]):
ch = self.font + self.str[i]
ch = self.font + <char>self.str[i]
y = max(y, ch.size.y)

i += 1
Expand Down

0 comments on commit 237299e

Please sign in to comment.