Scrollbar in listview working

This commit is contained in:
9and3r
2014-07-25 13:53:59 +02:00
parent 4d1420a15c
commit 33d0ac145f
5 changed files with 189 additions and 60 deletions

View File

@@ -1,6 +1,8 @@
from .screen_objects import ScreenObjectsManager
import logging
import pygame
from .touch_manager import TouchManager
from .touch_manager import TouchEvent
logger = logging.getLogger(__name__)
@@ -16,6 +18,7 @@ class ListView():
self.fonts = fonts
self.list_size = 0
self.list = []
self.scrollbar = False
self.set_list([])
@@ -23,20 +26,52 @@ class ListView():
def set_list(self, item_list):
self.list = item_list
self.list_size = len(item_list)
if self.max_rows < self.list_size:
self.scrollbar = True
self.screen_objects.add_scroll_bar("scrollbar", (self.pos[0]+self.size[0]-self.base_size,self.pos[1]), (self.base_size, self.size[1]),self.list_size,self.max_rows)
else:
self.scrollbar = False
self.load_new_item_position(0)
logger.error(self.list_size)
self.screen_objects.add_scroll_bar("scroll", (self.pos[0]+self.size[0]-self.base_size,self.pos[1]), (self.base_size, self.size[1]),self.list_size,self.max_rows)
def load_new_item_position(self, item_pos):
self.current_item = item_pos
if self.scrollbar:
self.screen_objects.clear("scrollbar")
else:
self.screen_objects.clear(None)
i = self.current_item
logger.error(self.max_rows)
while i < self.list_size and i - self.current_item < self.max_rows:
self.screen_objects.add_touch_object(str(i),self.fonts['dejavusans'], str(self.list[i]), (self.pos[0],self.pos[1]+self.base_size*i),None, (255, 255, 255))
z = 0
while i < self.list_size and z < self.max_rows:
self.screen_objects.add_touch_object(str(i),self.fonts['dejavusans'], str(self.list[i]), (self.pos[0],self.pos[1]+self.base_size*z),None, (255, 255, 255))
i += 1
z += 1
def render(self, surface):
self.screen_objects.render(surface)
def touch_event(self, touch_event):
if touch_event.type == TouchManager.click:
objects = self.screen_objects.get_touch_objects_in_pos(touch_event.current_pos)
if objects is not None:
for key in objects:
if key == "scrollbar":
direction = self.screen_objects.get_touch_object(key).touch(touch_event.current_pos)
if direction != 0:
self.move_to(direction)
def move_to(self, direction):
if direction == 1:
self.current_item += self.max_rows
if self.current_item + self.max_rows > self.list_size:
self.current_item = self.list_size - self.max_rows
self.load_new_item_position(self.current_item)
self.screen_objects.get_touch_object("scrollbar").set_item(self.current_item)
elif direction == -1:
self.current_item -= self.max_rows
if self.current_item < 0:
self.current_item = 0
self.load_new_item_position(self.current_item)
self.screen_objects.get_touch_object("scrollbar").set_item(self.current_item)